//
//  WXSTabBarController.m
//  SleepBooks
//
//  Created by rimi on 16/9/9.
//  Copyright © 2016年 rimi. All rights reserved.
//

#import "WXSTabBarController.h"
#import "WXSSleepViewController.h"
#import "WXSHypnosisViewController.h"
#import "WXSBooksViewController.h"
#import "WXSSettingViewController.h"

@interface WXSTabBarController ()

@property (nonatomic, strong) NSMutableArray * subViewControllerArray;

- (void)_initializeAppearance;//初始化

@end

@implementation WXSTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _initializeAppearance];
//    [self setupTabBarAttribute];
    
}

#pragma mark - private method

- (void)_initializeAppearance
{
    [self setTabBarItemWithViewController:[[WXSSleepViewController alloc]init] title:@"睡一觉" imageName:@"sleep"];
    [self setTabBarItemWithViewController:[[WXSHypnosisViewController alloc]init] title:@"听一曲" imageName:@"music"];
    [self setTabBarItemWithViewController:[[WXSBooksViewController alloc]init] title:@"记一笔" imageName:@"report"];
    [self setTabBarItemWithViewController:[[WXSSettingViewController alloc]init] title:@"设一下" imageName:@"set"];
    self.viewControllers = self.subViewControllerArray;
}

- (void)setTabBarItemWithViewController:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName
{
    vc.tabBarItem.title = title;
    [vc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:[UIColor colorWithWhite:0.655 alpha:1.000]} forState:0];
    [vc.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:[UIColor whiteColor]} forState:4];
    [vc.tabBarItem setImage:[[UIImage imageNamed:[NSString stringWithFormat:@"%@", imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [vc.tabBarItem setSelectedImage:[[UIImage imageNamed:[NSString stringWithFormat:@"%@-HL", imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:vc];
    [navi.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:14]}];
    [self.subViewControllerArray addObject:navi];
    
    //设置tabbar和navigationBar为透明
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kBaseWidth, kBaesHeight + 5)];
    [imageView setImage:[self createImageWithColor:[UIColor clearColor]]];
    [imageView setContentMode:UIViewContentModeScaleToFill];
    [self.tabBar insertSubview:imageView atIndex:0];
    [self.navigationController.navigationBar insertSubview:imageView atIndex:0];
    //    覆盖原生Tabbar的上横线
    [[UITabBar appearance] setShadowImage:[self createImageWithColor:[UIColor clearColor]]];
    [[UINavigationBar appearance] setShadowImage:[self createImageWithColor:[UIColor clearColor]]];
    //    背景图片为透明色
    [[UITabBar appearance] setBackgroundImage:[self createImageWithColor:[UIColor clearColor]]];
    [[UINavigationBar appearance] setBackgroundImage:[self createImageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
    
    self.tabBar.backgroundColor = [UIColor clearColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    //设置为半透明
    self.tabBarController.tabBar.translucent = YES;
    self.navigationController.navigationBar.translucent = YES;
}


-(UIImage*) createImageWithColor:(UIColor*) color{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

-(void)setupTabBarAttribute{
    //tabBar背景图片
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbarBG"]];
}

#pragma mark - lazy load

- (NSMutableArray *)subViewControllerArray
{
    if (_subViewControllerArray == nil)
    {
        _subViewControllerArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _subViewControllerArray;
}
@end
